package com.zhc.dejingapi.controller;


import com.alibaba.fastjson.JSONObject;
import com.zhc.dejingapi.pojo.common.CommonDelMoRoutingDataModel;
import com.zhc.dejingapi.pojo.common.CommonUpdMainModel;
import com.zhc.dejingapi.pojo.common.CommonUpdV2MainModel;
import com.zhc.dejingapi.pojo.param.QingCommonBackResult;
import com.zhc.dejingapi.service.syndb.ICommonDataDBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/dataDB")
public class CommonDataDBController {

    private final ICommonDataDBService commonDataDBService;

    public CommonDataDBController(ICommonDataDBService commonDataDBService1) {
        this.commonDataDBService = commonDataDBService1;
    }

    @PostMapping("/toDB")
    public QingCommonBackResult DataToDB(@RequestHeader(name = "apiKey") String apiKey,
                                          @RequestBody JSONObject param){
        QingCommonBackResult commonBackResult=new QingCommonBackResult();

        try{

            if(param==null){
                throw new Exception("参数为空");
            }

            if(!param.keySet().contains("dbTableName")){
                throw new Exception("出错：参数内没有dbTableName");
            }
            if(!param.keySet().contains("dbKeyField")){
                throw new Exception("出错：参数内没有dbKeyField");
            }
            if(!param.keySet().contains("data")){
                throw new Exception("出错：data不能为空");
            }
            commonBackResult=commonDataDBService.DataToDB(param);
        }
        catch (Exception e){
            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错："+e.getMessage()+"\n"+e.getStackTrace());

        }

        return commonBackResult;
    }


    @PostMapping("/genBarCodes")
    public QingCommonBackResult BatchGenBarCodes(@RequestHeader(name = "apiKey") String apiKey,
                                          @RequestBody JSONObject param){
        QingCommonBackResult commonBackResult=new QingCommonBackResult();

        try{

            if(param==null){
                throw new Exception("参数为空");
            }

            if(!param.keySet().contains("qtyField")){
                throw new Exception("出错：参数内没有qtyField");
            }
            if(!param.keySet().contains("devideQty")){
                throw new Exception("出错：参数内没有devideQty");
            }
            if(!param.keySet().contains("genBarCodeField")){
                throw new Exception("出错：参数内没有genBarCodeField");
            }
            if(!param.keySet().contains("data")){
                throw new Exception("出错：data不能为空");
            }
            if(!param.keySet().contains("backDataUrl")){
                throw new Exception("出错：backDataUrl不能为空");
            }
            // "genSerialNumField":"genSerialNum",
            if(!param.keySet().contains("genSerialNumField")){
                param.put("genSerialNumField","genSerialNum");
            }

            commonBackResult=commonDataDBService.BatchGenBarCodes(param);
        }
        catch (Exception e){
            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错："+e.getMessage()+"\n"+e.getStackTrace());

        }

        return commonBackResult;
    }

    //QingCommonBackResult UpdAIMAppFieldData(CommonUpdMainModel param);
    @PostMapping("/updAIMAppFld")
    public QingCommonBackResult UpdAIMAppFieldData(@RequestHeader(name = "apiKey") String apiKey,
                                                   @RequestBody CommonUpdMainModel param){
        QingCommonBackResult commonBackResult=new QingCommonBackResult();

        try{

            if(param==null){
                throw new Exception("参数为空");
            }

            commonBackResult=commonDataDBService.UpdAIMAppFieldData(param);
        }
        catch (Exception e){
            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错："+e.getMessage()+"\n"+e.getStackTrace());

        }

        return commonBackResult;
    }
    @PostMapping("/updAIMAppFldV2")
    public QingCommonBackResult UpdAIMAppFieldDataV2(@RequestHeader(name = "apiKey") String apiKey,
                                                   @RequestBody CommonUpdV2MainModel param){
        QingCommonBackResult commonBackResult=new QingCommonBackResult();

        try{

            if(param==null){
                throw new Exception("参数为空");
            }

            commonBackResult=commonDataDBService.UpdAIMAppFieldDataV2(param);
        }
        catch (Exception e){
            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错："+e.getMessage()+"\n"+e.getStackTrace());

        }

        return commonBackResult;
    }
    @PostMapping("/orderBackToLake")
    public QingCommonBackResult orderBackToLake(@RequestHeader(name = "apiKey") String apiKey,
                                             @RequestBody CommonDelMoRoutingDataModel param){
        QingCommonBackResult commonBackResult=new QingCommonBackResult();

        try{

            if(param==null){
                throw new Exception("参数为空");
            }


            commonBackResult=commonDataDBService.DelMoRouting(param,0);
        }
        catch (Exception e){
            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错："+e.getMessage()+"\n"+e.getStackTrace());
        }

        return commonBackResult;
    }
}
